package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.CurrentUser;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.customer.service.IServeProviderService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
@RequiredArgsConstructor
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {
    private final IAgencyCertificationService agencyCertificationService;
    private final IServeProviderService serveProviderService;


    @Override
    public PageResult<AgencyCertificationAudit> servePage(AgencyCertificationAuditPageQueryReqDTO agencyCertificationAuditPageQueryReqDTO) {
        // 企业名称
        String legalPersonName = agencyCertificationAuditPageQueryReqDTO.getLegalPersonName();
        // 法人名称
        String name = agencyCertificationAuditPageQueryReqDTO.getName();
        // 审核状态
        Integer auditStatus = agencyCertificationAuditPageQueryReqDTO.getAuditStatus();
        // 认证状态
        Integer certificationStatus = agencyCertificationAuditPageQueryReqDTO.getCertificationStatus();

        return PageHelperUtils.selectPage(agencyCertificationAuditPageQueryReqDTO, () -> this.baseMapper.queryPage(legalPersonName, name, auditStatus, certificationStatus));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(Long id, String rejectReason, Integer certificationStatus) {
        // 根据id查询机构认证审核信息
        AgencyCertificationAudit agencyCertificationAudit = this.getById(id);
        if (ObjectUtil.isNull(agencyCertificationAudit)) {
            throw new ForbiddenOperationException("认证审核信息不存在");
        }
        // 审核员审核，修改对应状态
        // 获取审核员信息
        Long userId = UserContext.currentUserId();
        CurrentUser currentUser = UserContext.getCurrentUser();
        String name = currentUser.getName();
        log.info("当前用户id：{}，用户名称：{}", userId, name);
        if (ObjectUtil.isNull(currentUser)) {
            throw new ForbiddenOperationException("用户不存在");
        }
        // 审核状态
        agencyCertificationAudit.setAuditStatus(1);
        // 认证状态
        agencyCertificationAudit.setCertificationStatus(certificationStatus);
        // 审核员id
        agencyCertificationAudit.setAuditorId(userId);
        // 审核员名称
        agencyCertificationAudit.setAuditorName(name);
        // 审核原因
        agencyCertificationAudit.setRejectReason(rejectReason);
        // 审核时间
        agencyCertificationAudit.setAuditTime(LocalDateTime.now());
        // 修改机构认证审核信息
        this.updateById(agencyCertificationAudit);
        // 如果认证状态为3，修改机构信息认证状态为3
        if (certificationStatus == 3) {
            // 修改机构信息认证状态为3
            agencyCertificationService.lambdaUpdate().eq(AgencyCertification::getId, agencyCertificationAudit.getServeProviderId())
                                      .set(AgencyCertification::getCertificationStatus, 3)
                                      .update();
        }
        // 如果认证状态为2，修改机构信息认证状态为2
        if (certificationStatus == 2) {
            // 修改机构信息认证状态为2
            agencyCertificationService.lambdaUpdate().eq(AgencyCertification::getId, agencyCertificationAudit.getServeProviderId())
                                      .set(AgencyCertification::getCertificationStatus, 2)
                                      .set(AgencyCertification::getCertificationTime, LocalDateTime.now())
                                      .update();
            // 修改serve_provider表添加机构姓名
            // 修改serve_provider首次设置状态为1
            serveProviderService.lambdaUpdate().eq(ServeProvider::getId, agencyCertificationAudit.getServeProviderId())
                                .set(ServeProvider::getName, agencyCertificationAudit.getName())
                                .set(ServeProvider::getSettingsStatus, 1)
                                .update();
        }

    }
}
